﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using System.Xml;
namespace MenuGame2DXNA
{
    /// <summary>
    /// 
    /// </summary>
    public class MyXNAButton : VisibleGameEntity
    {
        /// <summary>
        /// 
        /// </summary>
        private int _iSprite;
        /// <summary>
        /// Initializes a new instance of the <see cref="MyXNAButton"/> class.
        /// </summary>
        /// <param name="_contentManager">The _content manager.</param>
        /// <param name="strTexture">The STR texture.</param>
        /// <param name="nTextures">The n textures.</param>
        /// <param name="topLeft">The top left.</param>
        /// <param name="size">The size.</param>
        public MyXNAButton(ContentManager _contentManager, string[] strTexture, int nTextures, Vector2 topLeft, Vector2 size )
        {
            InitMyXNAButton(_contentManager, strTexture, nTextures, ref topLeft, ref size,"");
        }

        /// <summary>
        /// Inits my XNA button.
        /// </summary>
        /// <param name="_contentManager">The _content manager.</param>
        /// <param name="strTexture">The STR texture.</param>
        /// <param name="nTextures">The n textures.</param>
        /// <param name="topLeft">The top left.</param>
        /// <param name="size">The size.</param>
        /// <param name="rootFolder">The root folder.</param>
        private void InitMyXNAButton(ContentManager _contentManager, string[] strTexture, int nTextures, ref Vector2 topLeft, ref Vector2 size,string rootFolder)
        {
            TopLeft = topLeft;
            Size = size;

            List<List<Texture2D>> Textures = new List<List<Texture2D>>();
            Textures.Add(new List<Texture2D>());
            nObjectVisible = 1;
            for (int i = 0; i < nTextures; i++)
            {
                if (strTexture[i] == rootFolder)
                {
                    nObjectVisible++;
                    Textures.Add(new List<Texture2D>());
                    continue;
                }
                Textures[nObjectVisible - 1].Add(_contentManager.Load<Texture2D>(strTexture[i]));
            }

            ObjectVisible = new List<Sprite>();
            for (int i = 0; i < nObjectVisible; i++)
            {
                Sprite temSprite = new AutomatedSprite(Textures[i].ToArray(), TopLeft);
                ObjectVisible.Add(temSprite);
            }
            _iSprite = 0;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="MyXNAButton"/> class.
        /// </summary>
        /// <param name="_contentManager">The _content manager.</param>
        /// <param name="strTexturePrefix">The STR texture prefix.</param>
        /// <param name="nTextures">The n textures.</param>
        /// <param name="topLeft">The top left.</param>
        /// <param name="size">The size.</param>
        public MyXNAButton(ContentManager _contentManager, string strTexturePrefix, int nTextures, Vector2 topLeft, Vector2 size)
        {

            string[] strTexture = new string[nTextures];

            for (int i = 0; i < nTextures; i++)
            {
                strTexture[i] =  strTexturePrefix + (i+1).ToString("00");
            }
            InitMyXNAButton(_contentManager, strTexture, nTextures, ref topLeft, ref size,"");
            
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="MyXNAButton"/> class.
        /// </summary>
        /// <param name="_contentManager">The _content manager.</param>
        /// <param name="StrXml">The STR XML.</param>
        /// <param name="rootFolder">The root folder.</param>
        public MyXNAButton(ContentManager _contentManager, string StrXml,string rootFolder)
        {
            XmlDocument button = new XmlDocument();
            button.LoadXml(StrXml);
            XmlElement x = (XmlElement)button.SelectSingleNode("button/x");
            XmlElement y = (XmlElement)button.SelectSingleNode("button/y");
            Vector2 _topLeft =new Vector2( float.Parse(x.InnerText),float.Parse(y.InnerText));
            XmlElement width = (XmlElement)button.SelectSingleNode("button/width");
            XmlElement heigh = (XmlElement)button.SelectSingleNode("button/heigh");
            Vector2 _size = new Vector2(float.Parse(width.InnerText), float.Parse(heigh.InnerText));
            List<string> ListTextureTemp = new List<string>();
            XmlElement textures =(XmlElement) button.SelectSingleNode("button/textures");
            foreach (XmlElement xe in textures.ChildNodes)
            {
                ListTextureTemp.Add(rootFolder+xe.InnerText);
            }
            
            InitMyXNAButton(_contentManager, ListTextureTemp.ToArray(), ListTextureTemp.Count,ref _topLeft,ref _size,rootFolder);

        }
        /// <summary>
        /// Draws the specified game time.
        /// </summary>
        /// <param name="gameTime">The game time.</param>
        /// <param name="spriteBatch">The sprite batch.</param>
        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            ObjectVisible[_iSprite].Draw(gameTime,spriteBatch);
        }
        /// <summary>
        /// Updates the specified game time.
        /// </summary>
        /// <param name="gameTime">The game time.</param>
        /// <param name="color">The color.</param>
        public override void Update(GameTime gameTime, Color color)
        {
            ObjectVisible[_iSprite].Update(gameTime);
        }
        /// <summary>
        /// Sets the sprite.
        /// </summary>
        /// <param name="index">The index.</param>
        public void SetSprite(int index)
        {
            _iSprite = index;
        }
        
        
    }

    
}
